package com.openstack4j.qx.imp;

import java.util.List;

import org.openstack4j.api.Builders;
import org.openstack4j.api.OSClient.OSClientV2;
import org.openstack4j.api.compute.ServerService;
import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.compute.VolumeAttachment;
import org.openstack4j.model.storage.block.Volume;
import org.openstack4j.model.storage.block.Volume.Status;
import org.openstack4j.openstack.OSFactory;
import org.openstack4j.openstack.compute.internal.ServerServiceImpl;
import org.openstack4j.openstack.storage.block.domain.CinderVolume;

import com.openstack4j.qx.BSController;
import com.openstack4j.qx.pojo.BSParamArray;
import com.openstack4j.qx.pojo.BSResp;
import com.openstack4j.qx.pojo.BaseResp;
import com.openstack4j.qx.pojo.ResourceID;

public class BSControllerImp implements BSController {

	/**
	 * 创建块存储
	 */
	public BaseResp CreateBS(String paramFlag, String bSTemplateId, BSParamArray bSParamArray, String resourceID,
			String bSName, String appID, String appName) {
		//登录
		OSClientV2 os = OSFactory.builderV2()
                .endpoint("http://10.50.0.11:5000/v2.0")
	            .credentials("admin","ADMIN_PASS")
	            .tenantName("admin")
	            .authenticate();
		System.out.println("authentication complete");
		
		//判断选择磁盘性能,并传给volumeType
		String volume_type;
		if(bSParamArray.getTier()==0){   
			volume_type="SSD";   //性能型
		}else{
			volume_type="SATA";  //容量型
		}
	
		
		
		
		BSResp bSResp = new BSResp();
		
		
		//创建块存储volume	
		//按照bSParamArray.getVolNum，创建几个volume
		
	
		for(int i=0;i<bSParamArray.getVolNum();i++){
			Volume volume = os.blockStorage().volumes()
	             .create(Builders.volume()
	                .name(volume_type + "性能型磁盘")
	                .volumeType(volume_type)   // 配置容量型和性能型存储
	                .description(volume_type + "性能型磁盘")
	                .size(bSParamArray.getVolSize())
	                .build()
	             );
			bSResp.setBSID("成功创建volume"+volume.getId());
		}
		
		
		
		return bSResp;
	}

	
	
	/**
	 * 删除块存储
	 */
	public BaseResp DeleteBS(String bSID) {
		//登录
		OSClientV2 os = OSFactory.builderV2()
	            .endpoint("http://10.50.0.11:5000/v2.0")
	        .credentials("admin","ADMIN_PASS")
	        .tenantName("admin")
	        .authenticate();
		System.out.println("authentication complate");
		
		// delete specific volume by id
		ActionResponse actionResponse = os.blockStorage().volumes().delete(bSID);
		
		
		BaseResp baseResp=new BaseResp();
		baseResp.setFaultstring(actionResponse.getFault());
			
		return baseResp;
	
	}

	
	/**
	 * 查询块存储
	 */
	public BaseResp QueryBS(String bSID) {
		//登录
		OSClientV2 os = OSFactory.builderV2()
	            .endpoint("http://10.50.0.11:5000/v2.0")
	        .credentials("admin","ADMIN_PASS")
	        .tenantName("admin")
	        .authenticate();
		System.out.println("authentication complate");

		//实例化返回值对象
		BSResp bSResp = new BSResp();
		//安装volumeID来查询制定的volume
		Volume volume = os.blockStorage().volumes().get("volumeId");
		
		Status s = volume.getStatus();
			
		
		bSResp.setBSID(volume.getId());
		bSResp.setBSName(volume.getName());
		bSResp.setBSSize(volume.getSize());
		
		
		
		String status_name = volume.getStatus().name();
		if(status_name.equals("AVAILABLE")){
			bSResp.setTier(1);
		}else if(status_name.equals("ATTACHING")){
			bSResp.setTier(2);
		}else if(status_name.equals("BUILDING")){
			bSResp.setTier(3);
		}else if(status_name.equals("DETTACHING")){
			bSResp.setTier(4);
		}else{
			bSResp.setTier(5);
		}
		
		

		return bSResp;
	}

	
	
	/**
	 * 更新快存储
	 */
	public BaseResp UpdateBS(String bSID, int VolSize, String bSName) {
		//登录
		OSClientV2 os = OSFactory.builderV2()
                .endpoint("http://10.50.0.11:5000/v2.0")
            .credentials("admin","ADMIN_PASS")
            .tenantName("admin")
            .authenticate();
		System.out.println("authentication complate");
		
		
		// list specific volume by id
		Volume volume = os.blockStorage().volumes().get(bSID);   // parameter = bSID
		
		
		//update block storage name 
		ActionResponse ar1 = os.blockStorage().volumes().update(volume.getId(), bSName, bSName+"磁盘");
		
		//update block storage size
		ActionResponse ar2 = os.blockStorage().volumes().extend(volume.getId(), VolSize);
		
		BaseResp baseResp=new BaseResp();
		
		if(!ar1.isSuccess()){
			baseResp.setFaultstring(ar2.getFault());
		}else{
			baseResp.setFaultstring(ar1.getFault());
		}
		
		return baseResp;
	}

	
	/**
	 * 加载块存储
	 */
	public BaseResp AttachBS(String bSID, List<ResourceID> resourceID) {
		
		//登录
		OSClientV2 os = OSFactory.builderV2()
                .endpoint("http://10.50.0.11:5000/v2.0")
            .credentials("admin","ADMIN_PASS")
            .tenantName("admin")
            .authenticate();
		System.out.println("authentication complate");
			
		
	    // list volume by volume ID
		Volume volume = os.blockStorage().volumes().get("volumeId");
		
		//实例化ServerService 
		ServerService serverService = new ServerServiceImpl();
		
		//虚拟机ID，磁盘ID和设备名字
		VolumeAttachment volumeAttachment  = serverService.attachVolume("volumeId", bSID, "/dev/vdb");   
		
		BaseResp baseResp=new BaseResp();
		
		baseResp.setFaultstring("");
		
			
		
		return null;
	}

	
	/**
	 * 卸载块存储
	 */
	public BaseResp DetachBS(String bSID, List<ResourceID> resourceID) {
		//登录
		OSClientV2 os = OSFactory.builderV2()
                .endpoint("http://10.50.0.11:5000/v2.0")
            .credentials("admin","ADMIN_PASS")
            .tenantName("admin")
            .authenticate();
		System.out.println("authentication complate");
			
		
	    // list volume by volume ID
		Volume volume = os.blockStorage().volumes().get("volumeId");
		
		//实例化ServerService 
		ServerService serverService = new ServerServiceImpl();
		
		//虚拟机ID，磁盘ID和设备名字
		ActionResponse actionResponse = serverService.detachVolume("serverId", "attachmentId"); //attachmentID就是volumeID
		
		
		BaseResp baseResp=new BaseResp();
		if(actionResponse.isSuccess()){
			System.out.println("成功卸载volume");
		}else{
			baseResp.setFaultstring(actionResponse.getFault());
		}
		
		
		return baseResp;
	}

}
